package com.twu.biblioteca.domain;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Borrowable;
import com.twu.biblioteca.model.Customer;
import com.twu.biblioteca.model.Movie;

import java.util.ArrayList;
import java.util.List;

public class Library {

    private List<Borrowable> books = new ArrayList<Borrowable>();
    private List<Borrowable> movies = new ArrayList<Borrowable>();
    private List<Customer> customers = new ArrayList<Customer>();
    private List<Borrowable> borrowedBooks = new ArrayList<Borrowable>();
    private List<Borrowable> borrowedMovies = new ArrayList<Borrowable>();
    private Customer loginInCustomer = null;

    public void addBook(Book book) {
        books.add(book);
    }

    public Borrowable findBook(String bookId) {
        return find(bookId, books);
    }

    public void addMovie(Movie movie) {
        movies.add(movie);
    }

    public Borrowable findMovie(String movieId) {
        return find(movieId, movies);
    }

    public void moveMovieToBorrowed(Borrowable movie) {
        moveToBorrowed(movies, borrowedMovies, movie);
    }

    public void moveBookToBorrowed(Borrowable book) {
        moveToBorrowed(books, borrowedBooks, book);
    }

    private Borrowable find(String borrowableId, List<Borrowable> borrowables) {
        for (Borrowable borrowable : borrowables) {
            if (borrowable.getId().equals(borrowableId)) {
                return borrowable;
            }
        }
        return null;
    }

    private void moveToBorrowed(List<Borrowable> borrowables, List<Borrowable> borrowed, Borrowable borrowable) {
        borrowed.add(borrowable);
        borrowables.remove(find(borrowable.getId(), borrowables));

    }


    public void addCustomer(Customer customer) {
        customers.add(customer);
    }

    public List<Borrowable> getAllBooks() {
        return books;
    }

    public List<Borrowable> getAllMovies() {
        return movies;
    }

    public Borrowable findBorrowedBook(String bookId) {
        return find(bookId, borrowedBooks);
    }

    public void moveBookToBorrowable(Borrowable book) {
        moveToBorrowable(borrowedBooks, books, book);
    }

    private void moveToBorrowable(List<Borrowable> borroweds, List<Borrowable> borrowAbles, Borrowable borrowed) {
        borrowAbles.add(borrowed);
        borroweds.remove(findBorrowedBook(borrowed.getId()));
    }

    public Borrowable findBorrowedMovie(String movieId) {
        return find(movieId, borrowedMovies);
    }

    public void moveMovieToBorrowable(Borrowable movie) {
        moveToBorrowable(borrowedMovies, movies, movie);
    }

    public Customer getLoginInCustomer() {
        return loginInCustomer;
    }

    public void setLoginInCustomer(Customer loginInCustomer) {
        this.loginInCustomer = loginInCustomer;
    }

    public List<Customer> getCustomers() {
        return customers;
    }
}
